<?php
/**
 * Class UserModel
 * @package Common\Model
 * 用户资料模型——缓存、获取、统计
 */

namespace Common\Model;

class UserModel {
    private static $data = array();
    private  $redisDb = 1;
    private $prefix = 'sp_user_data_';
    private $uid;


    /**
     * @param string $uid  会员uid
     * @param bool $val  如果$val = true ,  则更新用户的缓存
     * @return $this
     *
     */
    public function id($uid='', $val=false)
    {
        $this->uid = $uid;
        empty($this->uid) && $this->uid = session('uid');

        if($this->uid)
        {
            if($val){
                $this->getDataFromSql();
            } else {
                if(empty(self::$data) || !isset(self::$data[$this->uid])){
                    self::$data[$this->uid] = unserialize(getRedis($this->redisDb)->get($this->prefix . $this->uid));
                    if(empty(self::$data[$this->uid]))
                    {
                        $this->getDataFromSql();
                    }
                }
            }
        }
        else
        {
            self::$data[$this->uid] = null;
        }
        return $this;
    }

    private function getDataFromSql(){
        $join = array();
        $join[] = 'left join '.C('DB_PREFIX').'user_info as info on U.uid=info.uid';
        $map = array();
        $map['U.uid'] = $this->uid;
        $field = 'U.account,U.password,U.is_focus,U.user_type,U.status,info.*';
        self::$data[$this->uid] = M()->table(C('DB_PREFIX').'user_login as U')->field($field)->join($join)->where($map)->find();
        self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'] = true;

        $this->update_cache();
    }

    //$is_admin 为true 时 则显示清晰图片 运营后台用到
    public function data($key, $value='',$is_admin=false)
    {
        if($key===null)
        {
            return self::$data[$this->uid];
        }
        elseif($value==='')
        {
            if((!isset(self::$data[$this->uid]['data'][$key]) || self::$data[$this->uid]['data'][$key]===null) && method_exists($this, $key) && !empty($this->uid))
            {
                self::$data[$this->uid]['data'][$key] = $this->$key();
                self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'] = true;
            }
            $this->update_cache();
            return self::$data[$this->uid]['data'][$key];
        }
        self::$data[$this->uid]['data'][$key] = $value;
        self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'] = true;
        $this->update_cache();
        return $this;
    }

    public function field($key=null, $value='')
    {
        if($key===null)
        {
            return self::$data[$this->uid];
        }
        elseif($value==='')
        {
            return self::$data[$this->uid][$key];
        }
        self::$data[$this->uid][$key] = $value;
        self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'] = true;
        $this->update_cache();
        return $this;
    }

    /**
     * 消息未读数
     * @return int
     */
    private function notReadNotice(){
        return M('notice')->where(array('uid'=>$this->uid,'is_read'=>2))->count();
    }

    /**
     * @param $uid
     * @return bool|string
     * 获取性别称谓
     */
    public function getGender($uid)
    {
        if(empty($uid)) return false;
        $gender = $this->id($uid)->field('gender');
        if(intval($gender)==1){
            return '先生';
        } else {
            return '女士';
        }
    }

    private function update_cache()
    {
        if(!empty(self::$data[$this->uid]) && self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'])
        {
            unset(self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit']);
            getRedis($this->redisDb)->set($this->prefix . $this->uid, serialize(self::$data[$this->uid]));
        }
    }

    public function del($uid){
        if(empty($uid)) return false;
        getRedis($this->redisDb)->del($this->prefix.$uid);
    }

    public function setDel($uidArr,$reason){
        foreach($uidArr as $uid){
            $data = array();
            $data['reason'] = $reason;
            $data['status'] = 3;

            $this->updateMultiFieldCache($uid,$data);
        }
    }

    public function setFreeze($uidArr,$time,$reason){
        foreach($uidArr as $uid){
            $data = array();
            /*$data['reason'] = $reason;
            $data['freeze_time'] = $time;*/
            $data['status'] = 2;

            $this->updateMultiFieldCache($uid,$data);
        }
    }

    /**
     * 批量更新字段缓存
     * @param $uid
     * @param $data
     * @return bool
     */
    public function updateMultiFieldCache($uid,$data){
        if(empty($uid) || empty($data)) return false;

        $this->id($uid)->field();
        if(empty(self::$data[$this->uid])) return false;

        self::$data[$this->uid] = array_merge(self::$data[$this->uid],$data);

        self::$data[$this->uid][$this->prefix.'_'.$this->uid.'edit'] = true;
        $this->update_cache();
    }
}
